
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Recepteur extends Thread
 {
  private int     numPort;
  private Process process;
  private Tampon  tamponReception;
    
  public Recepteur (Process _process, int numPort) 
   {
    this.process = _process;
    this.numPort = numPort;
    this.tamponReception = this.process.getTamponDeReception();
   }
    
  @Override
  public void run ()
   {
    try
     {
      ServerSocket serverSocket = new ServerSocket (numPort);
      System.out.println (process.getNumProcess() + " : Recepteur : Attente de connexion de l'emetteur");
      Socket socket = serverSocket.accept();
      ObjectInputStream fluxDEntree = new ObjectInputStream (socket.getInputStream ());            
      System.out.println (process.getNumProcess() + " : Recepteur : L'emetteur est connecte");
      boolean test=true;
	  while (test)
       {               
        //System.out.println (process.getNumProcess() + " : Recepteur : Attente message a recevoir");
        Message message = (Message) fluxDEntree.readObject();
        this.tamponReception.setElementTampon(message);
        MessageUtile messageTest = (MessageUtile) message;
        if(messageTest.getContenu().equals("Fin"))
        {
        	System.out.println("Fermeture du thread recepteur du processus " + process.getNumProcess());
        	test=false;
        }
        //System.out.println (process.getNumProcess() + " : Recepteur : Message recu");
       }
     }
    catch (ClassNotFoundException ex) 
	 {
      Logger.getLogger(Recepteur.class.getName()).log(Level.SEVERE, null, ex);
     }
    catch (IOException e1)
     {
      System.err.println(e1.getMessage());
     }
   }
 }
